
//
//  MiddleViewController.m
//  WD-Movies
//
//  Created by 贺泌钦 on 2019/1/23.
//  Copyright © 2019 chuck. All rights reserved.
//

#import "MiddleViewController.h"
#import "BuyViewController.h"
#import "MoviesDetailViewController.h"
#import "MiddleModel.h"
#import "MiddleTableViewCell.h"
@interface MiddleViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic , strong)UITableView *tbv;
@property(nonatomic,strong)NSMutableArray *datasource;
@property(nonatomic,strong)MiddleModel *model;
@end

@implementation MiddleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];//数据解析
    self.datasource =[NSMutableArray new];
    self.navigationItem.title = @"电影院列表";
    [self.view addSubview:self.tbv];
}
-(UITableView *)tbv{
    if (!_tbv) {
        _tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tbv.delegate = self;
        _tbv.dataSource = self;
        _tbv.rowHeight = 100;
        [_tbv registerNib:[UINib nibWithNibName:@"MiddleTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    }
    return _tbv;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.model.result.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MiddleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    赋值
    Model *model = self.model.result[indexPath.row];
    cell.imgView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.logo]]]];;
    cell.nameLa.text = model.name;
    cell.addressLa.text = model.address;
    cell.JvliLa.text = [NSString stringWithFormat:@"%@km",model.commentTotal];
    //添加选座购买按钮
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(330,60,80,30)];
    [btn setTitle:@"购买" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickbuy) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor grayColor];
    [cell addSubview:btn];
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tbv deselectRowAtIndexPath:indexPath animated:YES];
}

//数据请求部分
-(void)loadData{
//    http://172.17.8.100/movieApi/cinema/v1/findRecommendCinemas?userId=18&sessionId=15320748258726&page=1&count=10
//    NSDictionary *pdic = @{@"userId":@"18",@"sessionId":@"15320748258726",@"page":@"1",@"count":@"10"};
    
    AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
    //    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manger GET:@"http://172.17.8.100/movieApi/cinema/v1/findRecommendCinemas?userId=18&sessionId=15320748258726&page=1&count=10" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功%@",responseObject);
//        yymodel解析
        self.model = [MiddleModel yy_modelWithJSON:responseObject];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tbv reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@",error);
    }];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MoviesDetailViewController *detail = [[MoviesDetailViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
}


-(void)clickbuy{
    BuyViewController *buy = [[BuyViewController alloc]init];
    [self.navigationController pushViewController:buy animated:YES];
}

@end
